<template>
	<view>
		<button open-type="getUserInfo" @click="handleGetUserInfo" type="primary" plain> 登录 </button>
	</view>
</template>

<script>
	import {
		request
	} from "../../utils/api.js";
	export default {
		data() {
			return {
				
			};
		},
		methods:{
			handleGetUserInfo(e) {
				uni.getUserProfile({
					desc: 'weixin',
					success: res => {
						
						const {encryptedData, rawData, iv, signature}=res
						uni.login({
							success(res){
								const {code}=res
								console.log(code)
								console.log(encryptedData, rawData, iv, signature,code)
								const loginParams={encryptedData:encryptedData, rawData:rawData, iv:iv, signature:signature,code:code}
								
								request({url:"/users/wxlogin",data:loginParams,method:"post"}).then(res=>{
									console.log(res);
									const {token}=res;
									wx.setStorageSync("token", token);
									wx.navigateBack({
										delta: 1
									});
								})
							}
						})
						
					},
					fail: err => {
						console.log(err, '失败授权')
					}
				});
				
			}
		}
		
	}
</script>

<style lang="scss" scoped>
button{
  margin-top: 40rpx;
  width: 70%;
}
</style>
